package com.yw.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.yw.directory.domain.Directory;
import com.yw.directory.dto.ChildDTO;
import com.yw.directory.vo.DirectoryVO;
import com.yw.exception.BusinessException;
import com.yw.mapper.DirectoryMapper;
import com.yw.service.DirectoryService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author taj
 * @version 1.0
 */
@Service
public class DirectoryServiceImpl extends ServiceImpl<DirectoryMapper, Directory>
        implements DirectoryService {


    @Resource
    private DirectoryMapper directoryMapper;

    @Override
    public DirectoryVO getContents(Integer id) {
        DirectoryVO directoryVO = new DirectoryVO();

        //通过id得到当前总目录
        Directory category = this.getById(id);

        //创建DirectoryVO对象
        directoryVO.setId(category.getId());
        directoryVO.setName(category.getName());
        directoryVO.setParentId(category.getParentId());

        //递归获取子节点
        directoryVO.setChildDirectory(getChildDirectories(category.getId()));

        return directoryVO;
    }


    //递归获取所有子节点
    private List<DirectoryVO> getChildDirectories(Integer id){
        //通过id得到对应的叶子节点
        LambdaQueryWrapper<Directory> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Directory::getParentId,id);
        List<Directory> directoryList = this.list(wrapper);

        ArrayList<DirectoryVO> directories = new ArrayList<>();
        for (Directory directory : directoryList) {
            DirectoryVO one = new DirectoryVO();
            one.setId(directory.getId());
            one.setName(directory.getName());
            one.setParentId(directory.getParentId());
            //递归得到子节点的子节点
            one.setChildDirectory(getChildDirectories(directory.getId()));

            directories.add(one);
        }
        return directories;
    }

    @Override
    public Boolean addTop(String name) {
        //判断目录是否重名
        LambdaQueryWrapper<Directory> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Directory::getName,name);
        Directory directory = this.getOne(wrapper);
        if(directory != null){
            throw new BusinessException("该目录已存在，无法添加");
        }
        //新增目录
        Directory directory1 = new Directory();
        directory1.setName(name);
        directory1.setParentId(0);
        return save(directory1);
    }

    @Override
    public Boolean addChild(Directory directory) {
        //判断目录是否在同一级重名
        LambdaQueryWrapper<Directory> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Directory::getName,directory.getName()).
                eq(Directory::getParentId,directory.getParentId());
        Directory one = this.getOne(wrapper);
        if (one != null){
            throw new BusinessException("该级下已存在该目录，无法添加");
        }
        //新增目录
        Directory directory1 = new Directory();
        directory1.setParentId(directory.getParentId());
        directory1.setName(directory.getName());
        return save(directory1);
    }

    @Override
    public Boolean delChild(Integer id) {
        //判断该节点是不是叶子节点
        LambdaQueryWrapper<Directory> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Directory::getParentId,id);
        Directory directory = this.getOne(wrapper);
        if (directory !=  null){
            throw new BusinessException("不是叶子节点，不能删除");
        }
        return removeById(id);
    }

    @Override
    public Boolean updateChild(Directory directory) {
        //判断修改的目录名称是否重名
        LambdaQueryWrapper<Directory> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Directory::getName,directory.getName())
                .eq(Directory::getParentId,directory.getParentId());
        Directory one = this.getOne(wrapper);
        if (one != null){
            throw new BusinessException("该目录名已存在，无法修改");
        }
        //修改名字
        Directory directory1 = this.getById(directory.getId());
        directory1.setName(directory.getName());
        return updateById(directory1);
    }

    @Override
    public List<ChildDTO> getChildList(Integer id, String currentPath) {
        //根据id获取对应的孩子节点
        LambdaQueryWrapper<Directory> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Directory::getParentId,id);
        List<Directory> directories = this.list(wrapper);

        ArrayList<ChildDTO> childDTOS = new ArrayList<>();
        for (Directory directory : directories) {
            String path = currentPath == null ? directory.getName() :
                    currentPath + "/" + directory.getName();

            ChildDTO childDTO = new ChildDTO();
            //判断是否是叶子节点
            LambdaQueryWrapper<Directory> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(Directory::getParentId,directory.getId());
            List<Directory> directoryList = this.list(queryWrapper);

            if(directoryList.isEmpty()){
                //说明是叶子节点
                childDTO.setLeafId(directory.getId());
                childDTO.setPath(path);
                childDTOS.add(childDTO);
            }else {
                //不是叶子节点，递归获取
                childDTOS.addAll(getChildList(directory.getId(),path));
            }
        }
        return childDTOS;
    }

//    @Override
//    public String getPathById(Integer calagueId,String currentPath) {
//
//        //通过叶子id获取目录信息
//        Directory leaf = this.getById(calagueId);
//        String leafName = leaf.getName();
//
//        //通过父级id找到对应的父级
//        LambdaQueryWrapper<Directory> wrapper = new LambdaQueryWrapper<>();
//        wrapper.eq(Directory::getId,leaf.getParentId());
//        Directory directory = this.getOne(wrapper);
//
//        String path = currentPath == null ? leafName :
//                currentPath + "/" + directory.getName();
//        if(directory == null){
//            return path;
//        }else {
//            return getPathById(directory.getId(),path);
//        }
//
//    }


    @Override
    public String getPathById(Integer calagueId) {
        //通过叶子id得到目录信息
        Directory leaf = this.getById(calagueId);

        //如果当前是顶级目录，直接返回名称
        if(leaf.getParentId() == 0){
            return leaf.getName();
        }

        //递归获取父级路径,所得路径包含父级路径
        String path = getPathById(leaf.getParentId());
        return path + "/" + leaf.getName();

//        // 递归获取父级路径，不包含父级路径
//        Directory parent = this.getById(leaf.getParentId());
//        if (parent.getParentId() == 0) {
//            // 如果父级是顶级目录，直接返回当前目录名称
//            return leaf.getName();
//        } else {
//            // 否则继续递归获取父级路径
//            String path = getPathById(leaf.getParentId());
//            return path + "/" + leaf.getName();
//        }
    }
}


